local Lang = require("common.Lang")
local Broadcast = require("broadcast.Broadcast")
local ObjHuman = require("core.ObjHuman")
local InnerDataManagerFL = require("innerdata.InnerDataManagerFL")
local InnerDataDefine = require("innerdata.Define")
local RoleFFI = require("role.RoleFFI")
local ActivityLogicL = require("activity.ActivityLogicL")
local RankDefine = require("rank.RankDefine")

SHENBING_RANK = SHENBING_RANK or {}
HORSE_RANK = HORSE_RANK or {}
LEVEL_RANK = LEVEL_RANK or {}
POWER_RANK = POWER_RANK or {}
PET_RANK = PET_RANK or {}

function CG_RANK_WORSHIP(human, msg)
  if human.db.worshipLen >= RankDefine.WORSHIP_MAX_CNT then
    return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.RANK_WORSHIP_MAX_TIMES)
  end
  for i=0, human.db.worshipLen -1 do
    if RoleFFI.ffi_string(human.db.worship[i].uuid) == msg.uuid then
      return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.RANK_WORSHIP_ALREADY_DO)
    end
  end
  human.db.worship[human.db.worshipLen].uuid = msg.uuid
  human.db.worshipLen = human.db.worshipLen + 1  
  ObjHuman.addExp(human, RankDefine.WORSHIP_GET_EXP)
  local tmp = string.format(Lang.RANK_WORSHIP_SUCCESS, RankDefine.WORSHIP_GET_EXP, human.db.worshipLen, RankDefine.WORSHIP_MAX_CNT)
  Broadcast.sendBroadcast(human, Lang.DOWN, tmp)
  local info = {}
  info.uuid = msg.uuid
  info.type = msg.type
  InnerDataManagerFL.Send2WInnerData(human, InnerDataDefine.INNERDATA_TYPE_WOESHIP, info)
--  saveRoleSset({set._id = msg.uuid, set.worshipedTimes = })
  ActivityLogicL.updateActivityValue(human, "hero")  
end

function setRank(human, oType, info)
  if info[1] then
    if info[1].lv then
      LEVEL_RANK = info
    elseif info[1].horsePower then
      HORSE_RANK = info
    elseif info[1].sbPower then
      SHENBING_RANK = info
    elseif info[1].power then
      POWER_RANK = info
    elseif info[1].petPower then
      PET_RANK = info
    end
  end  
end

function getRank(type)
  if type == RankDefine.LV_RANK_TYPE then
    return LEVEL_RANK
  elseif type == RankDefine.POWER_RANK_TYPE then
    return POWER_RANK
  elseif type == RankDefine.SB_RANK_TYPE then
    return SHENBING_RANK
  elseif type == RankDefine.HORSE_RANK_TYPE then
    return HORSE_RANK
  elseif type == RankDefine.PET_RANK_TYPE then
    return PET_RANK
  end
end

function getHumanPowerRank(uuid)
  for i=1, #POWER_RANK do
    if POWER_RANK[i].uuid == uuid then
      return i
    end
  end
end

InnerDataManagerFL.Register(InnerDataDefine.INNERDATA_TYPE_RANK_INFO, setRank)


